package edu.gatech.cs2340.tigershroud;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
/**
 * Handles all creation calls..
 * @author
 *
 */
public class UserSQLiteHelper extends SQLiteOpenHelper 
{
	public static final String TABLE_USERS = "users";
	public static final String COLUMN_ID = "_id";
	public static final String COLUMN_UNAME = "uname";
	public static final String COLUMN_PASS = "pass";
	public static final String COLUMN_NAME = "name";
	public static final String COLUMN_EMAIL = "email";
	public static final String COLUMN_TYPE = "type";
	public static final String COLUMN_QUES = "question";
	public static final String COLUMN_ANSW = "answer";

	private static final String DATABASE_NAME = "users.db";
	private static final int DATABASE_VERSION = 1;

	// Database creation sql statement
	private static final String DATABASE_CREATE = "CREATE TABLE " + TABLE_USERS 
			+ "(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE, " 
			+ COLUMN_UNAME + " VARCHAR(30) NOT NULL, " 
			+ COLUMN_PASS + " VARCHAR(30) NOT NULL, "  
			+ COLUMN_NAME + " VARCHAR(30) NOT NULL, "  
			+ COLUMN_EMAIL + " VARCHAR(30) NOT NULL, " 
			+ COLUMN_TYPE + " VARCHAR(30) NOT NULL, " 
			+ COLUMN_QUES + " VARCHAR(100) NOT NULL, " 
			+ COLUMN_ANSW + " VARCHAR(100) NOT NULL)";

	/**
	 * Constructor for the helper class. Also makes the database. 
	 * @param context A context thing. Used for creation of the db by the 
	 * super class.
	 */
	public UserSQLiteHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}
	
	/**
	 * Creates the database via the create string.
	 * @param database The database to create (really updates b/c of android's
	 * database management system)
	 */
	@Override
	public void onCreate(SQLiteDatabase database) {
		database.execSQL(DATABASE_CREATE);
	}

	/**
	 * Upgrades the database to a new version. Wipes data.
	 * @param db The database to update
	 * @param oldVersion The old version of the db
	 * @param newVersion The new version of the db
	 */
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.w(UserSQLiteHelper.class.getName(),
				"Upgrading database from version " + oldVersion + " to "
						+ newVersion + ", which will destroy all old data");
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS);
		onCreate(db);
	}
	
	public String getCreateStatement()
	{
		return DATABASE_CREATE;
	}
}